﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net.Mail;
using ArtOfTest.WebAii.Silverlight.UI;
using ArtOfTest.WebAii.ObjectModel;

namespace Utilities
{
    public class WaitUtil
    {
        public static void waitTillContentsText(string expectedText)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                if (Utilities.GlobalObject.SILVERLIGHTAPP.Content.ToString().Contains(expectedText))
                {
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                    //Get finished
                    break;
                }

                #region Check if waiting so long - should be stop to wait
                stopWatch.Stop();
                string strElapsedTime = stopWatch.ElapsedMilliseconds.ToString();
                if (long.Parse(strElapsedTime) > Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND)
                {
                    //It should be error somewhere if waiting for more than 5 minutes
                    string logError = "Waiting exceeds defined timeout";
                    //Path
                    string path = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + "TIMEOUT_"
                                                                                        + Utilities.GlobalTestStep.strOwner
                                                                                        + "_" + Utilities.GlobalTestStep.strObject
                                                                                        + "_" + Utilities.GlobalTestStep.strAction
                                                                                        + "_" + Utilities.GlobalTestStep.strValidation
                                                                                        + ".jpg";

                    //Capture
                    ScreenUtil.captureScreen(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                    System.Threading.Thread.Sleep(1000);

                    //Attachement
                    Utilities.GlobalObject.MAIL.setAttachment(path);

                    //Throw exception
                    throw (new Exception(logError));
                }
                #endregion

                stopWatch.Start();
            }
        }
        
        public static void checkIfTimeWasOut(Stopwatch stopWatch)
        {
            stopWatch.Stop();
            string strElapsedTime = stopWatch.ElapsedMilliseconds.ToString();
            if (long.Parse(strElapsedTime) > Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND)
            {
                //It should be error somewhere if waiting for more than 5 minutes
                string logError = "Waiting exceeds defined timeout";
                //Path
                string path = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + "TIMEOUT_"
                                                                                    + Utilities.GlobalTestStep.strOwner
                                                                                    + "_" + Utilities.GlobalTestStep.strObject
                                                                                    + "_" + Utilities.GlobalTestStep.strAction
                                                                                    + "_" + Utilities.GlobalTestStep.strValidation
                                                                                    + ".jpg";

                //Capture
                ScreenUtil.captureScreen(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                System.Threading.Thread.Sleep(1000);

                //Attachement
                Utilities.GlobalObject.MAIL.setAttachment(path);

                //Throw exception
                throw (new Exception("here" + logError));
            }
            stopWatch.Start();            
        }
    }
}
